import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Product } from './product.entity';
import { RedisService } from 'src/redis/redis.service';


@Injectable()
export class ProductService {
    constructor(
        @InjectRepository(Product)
        private productRepository: Repository<Product>,
        private readonly redisService: RedisService,
    ) { }


    async findAll(): Promise<Product[]> {
        // 下面是测试完成的redis
        // this.redisService.cacheSet("idididi", "value!!!!", 300);
        // const cacheData = await this.redisService.cacheGet("idididi");
        // console.log(cacheData);

        return this.productRepository.find();
    }

    async addOne(name: string, price: number, mainType: string, secondType: string, introduce: string, info: string, files: object): Promise<Product> {
        const img_url = files["productImg"][0].path;
        const imgBigUrls = [];
        const imgSmallUrls = [];

        for(let i in files["rotationBig"]) {
            let item = files["rotationBig"][i];
            imgBigUrls.push(item.path);
        }
        for(let i in files["rotationSmall"]) {
            let item = files["rotationSmall"][i];
            imgSmallUrls.push(item.path);
        }

        const product = this.productRepository.create();
        product.name = name;
        product.price = price;
        product.type = mainType.split(",");
        product.type_es = secondType.split(",");
        product.img_url = img_url;
        product.imgBigUrls = imgBigUrls;
        product.imgSmallUrls = imgSmallUrls;
        product.introduce = introduce;
        product.info = info;
        product.time = new Date();

        console.log(product);
        return this.productRepository.save(product);
    }

    async updateOne(id: string, name: string, price: number, mainType: string, secondType: string, introduce: string, info: string, files: object): Promise<Product> {
        const product: Promise<Product> = this.productRepository.findOne({ where: { id: "Timber" } })
        const img_url = files["productImg"][0].path;
        const imgBigUrls = [];
        const imgSmallUrls = [];

        for(let i in files["rotationBig"]) {
            let item = files["rotationBig"][i];
            imgBigUrls.push(item.path);
        }
        for(let i in files["rotationSmall"]) {
            let item = files["rotationSmall"][i];
            imgSmallUrls.push(item.path);
        }

        if(await product === null) {
            return null;
        }
        if(name !== null) {
            (await product).name = name;
        }
        if(price !== null) {
            (await product).price = price;
        }
        if(mainType !== null) {
            (await product).type = mainType.split(",");
        }
        if(secondType !== null) {
            (await product).type_es = secondType.split(",");

        }
        if(img_url !== null) {
            (await product).img_url = img_url;
        }
        if(imgBigUrls !== null) {
            (await product).imgBigUrls = imgBigUrls;
        }
        if(imgSmallUrls !== null) {
            (await product).imgSmallUrls = imgSmallUrls;
        }
        if(introduce !== null) {
            (await product).introduce = introduce;
        }
        if(info !== null) {
            (await product).info = info;
        }

        return this.productRepository.save(await product);
    }   // updateOne

    async findOne(id: string): Promise<Product> {
        // redis 缓存中没的才去查 mysql
        const cacheData = await this.redisService.cacheGet(id);
        if(cacheData != null) {
            console.log("Redis 中有商品ID为：" + id + " 的数据，直接返回！");
            return cacheData;
        }
        const result = this.productRepository.find({ where: { id } })
        if((await result)[0] != null) {
            console.log("设置Redis缓存并返回数据！");
            this.redisService.cacheSet(id, JSON.stringify((await result)[0]));
            return (await result)[0];
        }
        return null;
    }


}
